package com.zzptc.tkfate;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;
import com.zzptc.tkfate.adapter.MyRvAdapter;
import com.zzptc.tkfate.databinding.ActivityMainBinding;
import com.zzptc.tkfate.entity.RecipeModel;
import com.zzptc.tkfate.entity.ResultsBean;
import com.zzptc.tkfate.service.MyAppService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    Retrofit retrofit=new Retrofit.Builder()
            .baseUrl("http://liyuankun.cn/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    MyAppService myAppService = retrofit.create(MyAppService.class);
    private static final String TAG = "MainActivity";
    ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
        //RecyclerView rv= findViewById(R.id.rv);
        getAllRecipe();
    }

    private void getAllRecipe() {
        Call<RecipeModel> call = myAppService.getAllRecipe(5);
        call.enqueue(new Callback<RecipeModel>() {
            @Override
            public void onResponse(Call<RecipeModel> call, Response<RecipeModel> response) {
                List<ResultsBean> results = response.body().getResults();
                for (ResultsBean r:
                     results) {
                    r.save();
                }
                MyRvAdapter adapter =new MyRvAdapter(MainActivity.this);
                binding.rv.setAdapter(adapter);
            }

            @Override
            public void onFailure(Call<RecipeModel> call, Throwable t) {
                Log.d(TAG, "onFailure: " + t);

            }
        });
    }
}
